import { _decorator, Component, Node } from 'cc';
import { IROOM_PLAYER, IWZQ_PLAYER } from 'db://assets/proto/proto';
import { Global } from 'db://assets/script/Global';
import { GameSocket } from 'db://assets/script/socket/GameSocket';
import { Button } from 'cc';
import { WzqProto } from './WzqProto';
const { ccclass, property } = _decorator;

@ccclass('WzqPlayer')
export class WzqPlayer extends Component {
    private _player: IWZQ_PLAYER;
    public roomPlayer: IROOM_PLAYER;

    public oping: boolean = false;
    @property({ type: Node })
    public opingNode: Node;

    public _ready: boolean = false;
    @property({ type: Node })
    public readyNode: Node;

    private isMe: boolean = false;

    private gameSocket: GameSocket;

    public set player(value: IWZQ_PLAYER) {
        this._player = value;
        this.roomPlayer = this._player.roomPlayer;
        this.isMe = this.roomPlayer.playerInfo.id == Global.me.id;
        if (this.isMe) {
            this.readyNode.OnClick(() => {
                if (this._ready) {
                    this.gameSocket.send(WzqProto.WZQ_UNREADY);
                } else {
                    this.gameSocket.send(WzqProto.WZQ_READY);
                }
            });
        } else {
            this.readyNode.Component(Button).destroy();
            this.readyNode.Label("已准备");
        }

        this.readyNode.active = this.isMe;
        this.node.LabelChild("name", this.roomPlayer.playerInfo.name);
        // this.node.SpriteChild("head", value.head);
        this.node.active = true;
    }

    protected onLoad(): void {
        this.gameSocket = Global.gameSocket;
    }

    playerExit() {
        this._player = null;
        this.roomPlayer = null;
        this.node.active = false;
    }

    // 到了玩家操作回合
    startOp() {
        this.oping = true;
        this.opingNode.active = true;
    }

    stopOp() {
        this.oping = false;
        this.opingNode.active = false;
    }

    ready() {
        this._ready = true;
        if (!this.isMe) {
            this.readyNode.active = true;
            return;
        }

        this.readyNode.LabelChild("label", "取消准备");
    }

    unready() {
        this._ready = false;
        if (!this.isMe) {
            this.readyNode.active = false;
            return;
        }

        this.readyNode.LabelChild("label", "准备");
    }

    startGame() {
        this.readyNode.active = false;
    }
}
